package com.dtf.admin.organization.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.dtf.admin.organization.form.SystemOrganizationForm;
import com.dtf.admin.organization.vo.SystemOrganization;
import com.dtf.core.common.service.BaseService;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.vo.Pagination;

/**
 * @Project: base
 * @Title: SystemOrganizationService.java
 * @Package com.sys.web.sysorganization.service
 * @Description: TODO
 * @author Administrator
 * @date 2015年6月28日 下午12:52:07
 * @Copyright 2015
 * @version V1.0
 */
@Service
public class SystemOrganizationService extends BaseService {
	
	public Map loadOrganizationTree(SystemOrganizationForm form){
		
		String sql = "select * from system_organization where 1=1 ";
		
		List params = new ArrayList();
		if (StringUtils.isEmpty(form.getNodeid())) {
			sql += " and parent_id='-1' ";
		}else{
			sql += " and parent_id=? ";
			params.add(form.getNodeid());
		}
		
		return super.getDaoSupport().queryListMapJGPage(sql, params, form.getPageSize(), form.getPage());
		
	}
	
	@Transactional
	public Map delete(SystemOrganizationForm form){
		String sql = "delete from system_organization where path like ? ";
		SystemOrganization organization = new SystemOrganization();
		organization.setOrganization_id(form.getId());
		organization = (SystemOrganization) super.get(organization);
		String path = organization.getPath();
		String parentId = organization.getParent_id();
		List params = new ArrayList();
		params.add(path + "%");
		//删除当前组织及下级组织
		super.getJdbcTemplate().update(sql, params.toArray());
		
		//变更父组织是否为叶子菜单
		sql = "update system_organization set is_leaf='true' where organization_id=?";
		params.clear();
		params.add(parentId);
		super.getJdbcTemplate().update(sql, params.toArray());
		
		return null;
	}
	
	public Pagination browseForPage(
			SystemOrganizationForm form,
			Pagination pagination) {
		StringBuffer hql = new StringBuffer();
		java.util.List<Object> params = new java.util.ArrayList<Object>();
		hql.append("from SystemOrganization ").append(
				"where 1=1 and status<>-99 ");
		if (form != null ) {
			SystemOrganization systemOrganization = form.getSystemOrganization();
			if (systemOrganization != null) {
				if (!Const.strIsEmpty(systemOrganization.getParent_id())) {
					hql.append("and parentId ").append("like ? ");
					params.add("%" + systemOrganization.getParent_id().trim()
							+ "%");
				}
				if (systemOrganization.getLevel() != 0) {
					hql.append("and level ").append("= ? ");
					params.add(systemOrganization.getLevel());
				}
				if (systemOrganization.getStatus() != 0) {
					hql.append("and status ").append("= ? ");
					params.add(systemOrganization.getStatus());
				}
				if (!Const.strIsEmpty(systemOrganization
						.getOrganization_id())) {
					hql.append("and organizationId ").append("like ? ");
					params.add("%"
							+ systemOrganization.getOrganization_id().trim()
							+ "%");
				}
				if (!Const.strIsEmpty(systemOrganization
						.getOrganization_name())) {
					hql.append("and organizationName ").append("like ? ");
					params.add("%"
							+ systemOrganization.getOrganization_name().trim()
							+ "%");
				}
			}
		}
//		return this.queryHQLForPage(hql.toString(), params, pagination);
		return null;
	}
}
